iT邦幫忙

0

Day 42 (PHP)

  • 分享至 

  • xImage
  •  

1.header的用法

(1)畫畫時
指定畫面Content-type網頁輸出,用image/jpeg型態

       header('Content-type: image/jpeg');

(2)輸出轉導向
輸出轉導向: 轉去這個網站

       header("Location: brad44.html")

思考方式 http

<head>
    <meta charset="UTF-8">
     //這個文件內容(content-type)是這個(text/html)型態
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>

2.上傳檔案,等比例縮小 imagecopyresized(作業)

https://www.php.net/manual/en/function.imagecopyresized


3.PHP的相加

  • 數學相加
    . 字串相加

4.物件 ->的

$this->speed


5.物件:重點在屬性,不是方法

  class Bike{
    private $speed = 0;

    function upSpeed()
    {
        $this->speed = $this->speed < 1 ? 1 : $this->speed * 1.2;  //->的 
    }
    function downSpeed()
    {
        $this->speed = $this->speed < 1 ? 0 : $this->speed * 0.7;
    }
    function getSpeed()
    {
        return $this->speed;
    }
  }

6.創物件

     $myBike = new Bike; //new 新創物件

7.物件 - 繼承extends(好事發揚光大,壞事變更小)

若函式內甚麼都沒寫,會完全跟爸爸(Scooter)一樣

    // Scooter is-a Bike
    class Scooter extends Bike{
    private $gear = 0;

    function chGear($gear = 0)
    {
        if ($gear >= 0 && $gear <= 7) {
            $this->gear = $gear;
        }
    }

    function upSpeed()
    {
        $this->speed = $this->speed < 1 ? 1 : $this->speed * ($this->gear * 1.2);
    }

    function dump()
    {
        var_dump($this);
    }
}

8.物件 - private

只能用在這個函式中,可以拿來使用,但不能繼承extends

  class Bike{
    private $speed = 0;

    function upSpeed()
    {
        $this->speed = $this->speed < 1 ? 1 : $this->speed * 1.2;  //->的 
    }
    function downSpeed()
    {
        $this->speed = $this->speed < 1 ? 0 : $this->speed * 0.7;
    }
    function getSpeed()
    {
        return $this->speed;
    }
  }

開放繼承子類別(一個子) - protected

    class Bike{
    // Bike has-a speed
    protected $speed = 0;  //protected

    // Bike has-a upSpeed()
    function upSpeed()
    {
        $this->speed = $this->speed < 1 ? 1 : $this->speed * 1.2;
    }
    function downSpeed()
    {
        $this->speed = $this->speed < 1 ? 0 : $this->speed * 0.7;
    }
    function getSpeed()  //辨識protected
    {
        return $this->speed;
    }
    }

都沒有設定就是全部都可以繼承用


9.建構式/建構子/建構方法/Constructor => 物件初始化

__construct進行屬性的初始化
原本物件已存在,再進行屬性的初始化(給定義)

   class Person
    {

    //屬性,屬性,屬性
    private $name, $bike, $scooter;
    function __construct($name)
    {
    //定義屬性
        $this->name = $name;
        $this->bike = new Bike;
        $this->scooter = new Scooter;
    }

    function getName()
    {
        return $this->name;
    }
    function getBike()
    {
        return $this->bike;
    }
    function getScooter()
    {
        return $this->scooter;
    }
}

10.MySQL - MySQLi

Abstraction Layers > 連接各家資料庫來源(有整合的)
DBA — Database (dbm-style) Abstraction Layer
ODBC — ODBC (Unified) ===>整合各家程式語言系統(DSN)
PDO — PHP Data Objects

Vendor Specific Database Extensions > 直接連線各家的
CUBRID
dBase
Firebird/InterBase
FrontBase
IBM DB2 — IBM DB2, Cloudscape and Apache Derby
MongoDB — MongoDB driver
MySQL — MySQL Drivers and Plugins
OCI8 — Oracle OCI8
PostgreSQL
SQLite3
SQLSRV — Microsoft SQL Server Driver for PHP

https://www.php.net/manual/en/refs.database.php

MySQL 列表
https://www.php.net/manual/en/set.mysqlinfo.php

MySQLi MySQL的強化版
https://www.php.net/manual/en/book.mysqli.php

MySQLi > The mysqli class MySQL類別
https://www.php.net/manual/en/class.mysqli.php

MySQLi > The mysqli class > mysqli::__construct 連接特定資料庫


11.PHP要寫資料庫時,要開MySQL

看看組態檔brad02有沒有支援到 MySQLi 外掛


12.建立 連結資料庫MySQL

<?php
    $mysqli = new mysqli('127.0.0.1', 'root', 'root', 'class', 3306);  //建立SQL物件
    $mysqli->set_charset("utf8"); //編碼

    //準備 要執行的SQL(抓好$sql的資料型態)
    $stmt = $mysqli->prepare($sql); //物件式
    //$stmt = mysqli_prepare($mysqli, $sql); //方法式

    $stmt->execute(); //執行SQL
?>

13.查看有無連線到資料庫 var_dump()

    $mysqli = new mysqli('127.0.0.1', 'root', 'root', 'class', 3306);
    echo gettype($mysqli); //object0
    var_dump($mysqli);
    // object(mysqli)#1 (18) { 
    //     ["affected_rows"]=> int(0) 
    //     ["client_info"]=> string(13) "mysqlnd 7.4.1" 
    //     ["client_version"]=> int(70401) 
    //     ["connect_errno"]=> int(0) 
    ===>無錯誤 連線上

    //     ["connect_error"]=> NULL 
    //     ["errno"]=> int(0) 
    //     ["error"]=> string(0) "" 
    //     ["error_list"]=> array(0) { } 
    //     ["field_count"]=> int(0) 
    //     ["host_info"]=> string(20) "127.0.0.1 via TCP/IP" 
    //     ["info"]=> NULL 
    //     ["insert_id"]=> int(0) 
    //     ["server_info"]=> string(6) "5.7.24"  
    ===>Server version: 5.7.24 - MySQL Community Server (GPL)

    //     ["server_version"]=> int(50724) 
    //     ["sqlstate"]=> string(5) "00000" 
    //     ["protocol_version"]=> int(10) 
    //     ["thread_id"]=> int(5) 
    //     ["warning_count"]=> int(0) } 0

https://ithelp.ithome.com.tw/upload/images/20210807/20137684RHvABe7SWC.png


14.增加TABLE資料

prepare()是抓好$sql的資料型態,之後
bind_param 直接綁定 (必須按照INSERT INTO(DELETE、UPDATE..)順序=======>重要)
execute()執行SQL
就可以放資料

    $sql = "INSERT INTO students (cName,cSex,cBirthday) VALUES (?,?,?,?)"; 
    //VALUES (?,?,?,?) 預先準備不要直接給

    //準備 要執行的SQL(抓好$sql的資料型態)
    $stmt = $mysqli->prepare($sql); //物件式

    $cName = 'Brad';
    $cSex = 'M';
    $cBirthday = '1999-01-02';
    $stmt->bind_param('sss', $cName, $cSex, $cBirthday); 
    //綁定參數bind_param(資料型態s字串 i整數)

必要給的資料要給,會自動遞增的無須寫入
https://ithelp.ithome.com.tw/upload/images/20210807/20137684lxyzo5SUEt.png

Q:為什麼 刪掉後再新增的資料id不會遞補前面被刪號碼??
A:自動遞增是一個變數,只會前進不會後退
如果想要調整自動遞增的值
查詢:php mysql reset increment value
https://www.studentstutorial.com/php/autoincrement-reset.php

mysqli_query($conn,"ALTER TABLE category AUTO_INCREMENT = 1");

15.資料庫語法 - 顯示錯誤

https://www.php.net/manual/en/function.mysql-errno.php

    //錯誤訊息顯示
    echo $mysqli->errno . '<br />'; //0 無錯誤
    echo $mysqli->error . '<br />';

16.刪除TABLE資料

    $sql = "DELETE FROM students WHERE cID > ?";

    $stmt = $mysqli->prepare($sql);

    $delId = 10;  //cID 10號不要
    $stmt->bind_param('i', $delId); 
    $stmt->execute(); //執行SQL

17.修改TABLE資料

    //修改 UPDATE SET WHERE 條件式
    $sql = "UPDATE students SET ch =?, eng=?, math=? WHERE cID = ?";

    $stmt = $mysqli->prepare($sql);


    //修改45號
    $updateId = 45;
    $ch = rand(50, 80);
    $eng = rand(50, 80);
    $math = rand(50, 80);
    $stmt->bind_param('iiii', $ch, $eng, $math, $updateId);
    $stmt->execute(); //執行SQL

18.查詢TABLE資料v_1(prepare版本)

選擇的結果種類
mysqli_stmt — The mysqli_stmt class
https://www.php.net/manual/en/book.mysqli.php

     <?php
     $mysqli = new mysqli('127.0.0.1', 'root', 'root', 'class', 3306);
     $mysqli->set_charset("utf8");

     $sql = "SELECT cName,ch,eng,math FROM students";   //1.MySQL I 敘述句


     // 兩種寫法都一樣 準備 要執行的SQL      //2.拿到MySQL I 敘述句
     $stmt = $mysqli->prepare($sql);
     //$stmt = mysqli_prepare($mysqli, $sql);
     

     $stmt->execute(); //執行SQL     //3.執行拿到MySQL I 敘述句

     $stmt->store_result();     //4.把結果儲存

     echo $stmt->num_rows . '<br />';     //5.打開看 選擇的結果

     $stmt->bind_result($v1, $v2, $v3, $v4);     //6.限定看哪幾種資料
                                                 //bind_result查看資料的綁定參數,必須按照SELECT順序
                
     while ($stmt->fetch())                      //7.一個一個抓出來看fetch()   
         echo "{$v1} : {$v2} : {$v3} : {$v4}<br />";
     }

19.查詢TABLE資料v_2(陣列版本)

mysqli_query 製作成物件後,抓全部
https://www.php.net/manual/en/mysqli.query.php

看要出來甚麼值
https://www.php.net/manual/en/class.mysqli-result.php
陣列
https://www.php.net/manual/en/mysqli-result.fetch-array.php

     $mysqli = new mysqli('127.0.0.1', 'root', 'root', 'class', 3306);
     $mysqli->set_charset("utf8");

     //mysqli_query 製作成物件後,抓全部(物件) 2.MySQL I 敘述句
     $result = $mysqli->query('SELECT * FROM students '); 


     $row = $result->fetch_array();  //3. 看要出來甚麼值 用陣列的方式拿值

     //4.製作成迴圈,讓全部資料被抓取
     while ($rows = $result->fetch_array()) {                
         echo "{$rows['cID']} : {$rows['cName']} : {$rows['ch']}<br />";
       
     }

20.查詢TABLE資料v_2(陣列版本)

mysqli_query 製作成物件後,抓全部
https://www.php.net/manual/en/mysqli.query.php

看要出來甚麼值
https://www.php.net/manual/en/class.mysqli-result.php
物件
https://www.php.net/manual/en/mysqli-result.fetch-object.php

     $mysqli = new mysqli('127.0.0.1', 'root', 'root', 'class', 3306);
     $mysqli->set_charset("utf8");
  
     $result = $mysqli->query('SELECT * FROM students '); // 2.MySQL I 敘述句

     $row = $result->fetch_object();  // 3.換成物件
     //$row = $result->fetch_array();

     // 4.全部抓出來
     while ($rows = $result->fetch_object()) {
         //物件抓法
         echo "{$rows->cID} : {$rows->cName}<br />";
         //echo "{$rows[0]} : {$rows[1]} : {$rows[7]}<br />";
     }     

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言